﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MediaAutomatorLib.ExternalPrograms
{
    public class XbmcProgram
    {
        private readonly String _hostname;
        private readonly int _port;
        private readonly String _username;
        private readonly String _password;

        public XbmcProgram(String hostname, int port, String username, String password)
        {
            _hostname = hostname;
            _port = port;
            _username = username;
            _password = password;
        }

        public void UpdateVideoLibrary()
        {
            try
            {
                Console.WriteLine("Update video library");
                WebClient webClient = new WebClient();
                webClient.Credentials = new System.Net.NetworkCredential(_username, _password);
                Stream stream = webClient.OpenRead(
                    string.Format("http://{0}:{1}/xbmcCmds/xbmcHttp?command=ExecBuiltIn&parameter=XBMC.updatelibrary(video)", _hostname, _port));
                TextReader reader = new StreamReader(stream);
                String request = reader.ReadToEnd();
                Console.WriteLine(request);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
/*            catch (WebException ex)
            {
                if (ex.Response is HttpWebResponse)
                {
                    switch (((HttpWebResponse)ex.Response).StatusCode)
                    {
                        case HttpStatusCode.NotFound:

                            break;

                        default:
                            throw ex;
                    }
                }
            }
  */              
            
        }
    }
}
